package com.lpd100.china.chenchen.hj;

import org.apache.commons.lang3.StringUtils;

/**
 * 计算字符串最后一个单词的长度，单词以空格隔开，字符串长度小于5000。（注：字符串末尾不以空格为结尾）
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/8/25
 */
public class HJ1 {
    public static final HJ1 INSTANCE = new HJ1();

    public int action(String input) {
        System.out.println("input: [" + input + "]");
        if (StringUtils.isBlank(input)) {
            return 0;
        }
        if (input.length() > 50) {
            throw new RuntimeException("字符串长度小于5000");
        }
        if (input.matches(".+ +")) {
            throw new RuntimeException("字符串末尾不以空格为结尾");
        }
        String[] words = input.split(" ");
        return words[words.length - 1].length();
    }

    public static void main(String[] args) {
        String input = "hello world";
        System.out.println(HJ1.INSTANCE.action(input));
        input = "hello world    ";
        System.out.println(HJ1.INSTANCE.action(input));
        input = "hello world hello world hello worldhello worldhello worldhello worldhello worldhello worldhello world";
        System.out.println(HJ1.INSTANCE.action(input));
    }

}


